package cn.edu.seu.ssny.service.impl;

import cn.edu.seu.ssny.dao.IProjectDao;
import cn.edu.seu.ssny.dao.support.IBaseDao;
import cn.edu.seu.ssny.entity.Enterprise;
import cn.edu.seu.ssny.entity.Project;
import cn.edu.seu.ssny.service.IProjectService;
import cn.edu.seu.ssny.service.support.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业表
 */
@Service
public class ProjectServiceImpl extends BaseServiceImpl<Project, Integer> implements IProjectService {

    @Autowired
    private IProjectDao projectDao;

    @Override
    public IBaseDao<Project, Integer> getBaseDao() {
        return this.projectDao;
    }


    @Override
    public Project findByName(String name) {
        return projectDao.findByName(name);
    }

    @Override
    public Project findByIdentification(String identification) {
        return projectDao.findByIdentification(identification);
    }

    @Override
    public List<Project> findAllByEnterprise(Enterprise enterprise) {
        return projectDao.findAllByEnterprise(enterprise);
    }


    @Override
    public void saveOrUpdate(Project project, Enterprise enterprise) {
        if (project.getId() != null) {
            Project dbProject = projectDao.findOne(project.getId());
            dbProject.setIdentification(project.getIdentification());
            dbProject.setName(project.getName());
            dbProject.setCity(project.getCity());
            dbProject.setCreateTime(project.getCreateTime());
            dbProject.setDescription(project.getDescription());
            dbProject.setMaxColdLoad(project.getMaxColdLoad());
            dbProject.setMaxHotLoad(project.getMaxHotLoad());
            dbProject.setArea(project.getArea());
            update(dbProject);
        } else {
            project.setEnterprise(enterprise);
            save(project);
        }
    }

    @Override
    public void saveDisplay(Project project) {
        Project dbProject = find(project.getId());
        dbProject.setPanel(project.getPanel());
        dbProject.setImage(project.getImage());
        dbProject.setFont(project.getFont());
        dbProject.setFontSize(project.getFontSize());
        update(dbProject);
    }

    @Override
    public void savePanel(Project project) {
        Project dbProject = find(project.getId());
        dbProject.setPanelX(project.getPanelX());
        dbProject.setPanelY(project.getPanelY());
        update(dbProject);
    }

}